/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import org.dctmcommons.IParamCheck;

import com.documentum.fc.methodserver.DfStandardJobArgumentNames;
import com.documentum.fc.methodserver.IDfMethod;

/**
 * Contains helper methods for testing Documentum Java methods.
 * @author Mark Davidson
 */
public class MethodTestHelper {

	/** The key for the username parameter. */
	public static final String KEY_USERNAME = DfStandardJobArgumentNames
													.USER_NAME;
	
	/** The key for the password/ticket parameter. */
	public static final String KEY_PASSWORD = "password";
	
	/** The key for the domain parameter. */
	public static final String KEY_DOMAIN = "domain";
	
	/** The key for the docbase name parameter. */
	public static final String KEY_REPOSITORY = DfStandardJobArgumentNames
													.DOCBASE_NAME;

	/** The parameter check instance. */
	private static final IParamCheck paramCheck = ParamCheck.getInstance();

	/**
	 * Runs a method.
	 * @param method The method
	 * @param args The method arguments in standard Method argument format
	 * @throws Exception The repository exception
	 */
	public String runMethod(final IDfMethod method,
							final Map<String, String[]> args) throws Exception {
		
		paramCheck.checkObject(method, "method");
		
		final StringWriter sw = new StringWriter();
		final PrintWriter out = new PrintWriter(sw);
		try {
			method.execute(args, out);
		} catch (Throwable t) {
			System.out.println(t.getLocalizedMessage());
			t.printStackTrace();
		}
		return out.toString();
	}
	
	/**
	 * Create an {@link Map} of standard method arguments.
	 * @param repository The repository
	 * @param username The username
	 * @param password The password
	 * @param domain The domain
	 * @return The method arguments
	 */
	public Map<String, String[]> createDefaultArgs(final String repository,
												   final String username,
												   final String password,
												   final String domain) {
		
		paramCheck.checkString(repository, "repository");
		
		final Map<String, String[]> args =
										new LinkedHashMap<String, String[]>();
		args.put(KEY_REPOSITORY, new String[] { repository });
		if (username != null) {
			args.put(KEY_USERNAME, new String[] { username });
		}
		if (password != null) {
			args.put(KEY_PASSWORD, new String[] { password });
		}
		if (domain != null) {
			args.put(KEY_DOMAIN, new String[] { domain });
		}

		return args;
	}
}
